//
//  PGBarItem.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-4-17.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGBarItem.h"
#import "PGTabBar.h"

#define PurpleColor COLOR(91, 68, 106)

@interface PGBarItem ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation PGBarItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithTitle:(NSString *)title image:(UIImage *)image highlightImage:(UIImage *)highlightImage tag:(NSInteger)tag {
    self = [self initWithFrame:CGRectMake(0, 0, PGBarItemWidth, 40)];
    self.layer.cornerRadius = 5;
//    self.backgroundColor = [UIColor yellowColor];
    _title = title;
    _image = image;
    _highlightImage = (highlightImage == nil ? [self grayImage:image] : highlightImage);
    [self initSubView];
    [self setDefaultValues];
    return self;
}

- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag {
    return [self initWithTitle:title image:image highlightImage:nil tag:tag];
}

- (void)setDefaultValues {
    _label.textAlignment = NSTextAlignmentCenter;
    _label.font = [UIFont systemFontOfSize:14];
    _label.backgroundColor = [UIColor clearColor];
    _label.textColor = PurpleColor;//[UIColor darkTextColor];
    
    _tipsLabel.textColor = [UIColor whiteColor];
    _tipsLabel.layer.backgroundColor = [[UIColor redColor] CGColor];
    if (!IsiOS7Over) {
        _tipsLabel.backgroundColor = [UIColor redColor];
    }
    _tipsLabel.layer.cornerRadius = 7;
    _tipsLabel.font = [UIFont systemFontOfSize:12];
    _tipsLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)initSubView {
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
    _imageView.image = _image;
    _imageView.highlightedImage = _highlightImage;
    [self addSubview:_imageView];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(33, 0, 33, 14)];
    _label.text = _title;
    [self addSubview:_label];
    
    self.tipsLabel = [[UILabel alloc] initWithFrame:CGRectZero];    
//    _tipsLabel.text = @"2";
    [self addSubview:_tipsLabel];
    _tipsLabel.hidden = YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _imageView.centerY = self.height*0.5;
    
    _label.centerY = self.height*0.5;
    
    if (_tipsLabel.hidden == NO) {
        //[_tipsLabel sizeToFit];
        _tipsLabel.width = _tipsLabel.width>7 ? _tipsLabel.width+4 : 15;
        _tipsLabel.top = _tipsLabel.left = 0;
    }
}

- (void)setItemTitle:(NSString *)title {
    _title = title;
    _label.text = _title;
    [_label sizeToFit];
}

- (void)setBadgeValue:(NSString *)badgeValue {
    _badgeValue = badgeValue;
    if ([badgeValue intValue] <= 0) {
        _tipsLabel.hidden = YES;
        return;
    }
    _tipsLabel.text = badgeValue;
    _tipsLabel.hidden = NO;
    [_tipsLabel sizeToFit];
}

- (void)touchDown {
    _imageView.highlighted = YES;
    _label.textColor = [UIColor grayColor];
    self.backgroundColor = [UIColor lightGrayColor];
}

- (void)touchUp {
    _imageView.highlighted = NO;
    _label.textColor = PurpleColor;
    self.backgroundColor = [UIColor clearColor];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchDown];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchUp];
//    [self responseTouchEvent];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchUp];
    [self responseTouchEvent];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchUp];
}

- (void)responseTouchEvent {
//    self.isSelected = !_isSelected;
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedAtPGBarItem:)]) {
        [_delegate didSelectedAtPGBarItem:self];
    }
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
}

- (UIImage *)grayImage:(UIImage *)sourceImage {
    int bitmapInfo = kCGImageAlphaOnly;
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();      // bits per component
    CGContextRef context = CGBitmapContextCreate (nil, width, height, 8, 0, colorSpace, bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,
                       CGRectMake(0, 0, width, height), sourceImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
}

@end
